package cn.xinfei.xdecision.engine.runner.executor.strategy.branch;

import cn.xinfei.xdecision.common.model.component.strategyout.StrategyOutput;
import cn.xinfei.xdecision.common.model.enginex.po.EngineNode;
import cn.xinfei.xdecision.common.utils.enums.enginex.NodeTypeEnum;
import cn.xinfei.xdecision.common.vo.FieldInfoVo;
import cn.xinfei.xdecision.engine.runner.context.BranchTypeEnum;
import cn.xinfei.xdecision.engine.runner.context.PipelineContextHolder;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContext;
import cn.xinfei.xdecision.engine.runner.context.XDecisionContextHolder;
import cn.xinfei.xdecision.engine.runner.executor.NodeHandler;
import cn.xinfei.xdecision.engine.runner.executor.XRunner;
import cn.xinfei.xdecision.engine.runner.executor.handler.ChampionChallengeHandler;
import cn.xinfei.xdecision.engine.runner.executor.model.StateTransmitModel;
import cn.xinfei.xdecision.engine.runner.executor.strategy.EngineNodeRunner;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static cn.xinfei.xdecision.constant.EngineResultEnum.EXCEPTION_NODE_NULL_END;

/**
 * 冠军挑战节点
 */
@XRunner
@Service
@Slf4j
public class ChampionChallenge extends EngineNodeRunner {


    @Autowired
    private XDecisionContextHolder xDecisionContextHolder;

    @Autowired
    private ChampionChallengeHandler championChallengeHandler;

    @Override
    public String getServiceName() {
        return EngineNodeRunner.PRE + NodeTypeEnum.CHAMPION_CHALLENGE.getType();
    }

    public Map<String, FieldInfoVo> getInputFieldInfo(NodeHandler nodeHandler) {
        return null;
    }
    @Override
    public Map<String, StrategyOutput> getTerminalOutputFieldInfo(NodeHandler nodeHandler) {

        Map<String, StrategyOutput> fieldVos = new HashMap<>();

        return fieldVos;
    }
    @Override
    public Map<String, StrategyOutput> getOutputFieldInfo(NodeHandler nodeHandler) {
        return null;
    }

    public static void main(String[] args) {
//        String code="xxx";
//        String code="xxx_";
//        String code="xxx_1";
        String code="xxx_1_2";

        String[] split = code.split("_");
        String codeIndex=split[split.length-1];
        System.out.println(codeIndex);
    }
    /**
     * 返回冠军分支对应的下一个节点
     *
     * @param nodeHandler
     */
    @Override
    public StateTransmitModel runNode(NodeHandler nodeHandler) {
        EngineNode engineNode = nodeHandler.getEngineNode();
        String nodeCode = engineNode.getNodeCode();
        NodeHandler championNode = null; // 冠军节点code
        String nodeJson = engineNode.getNodeJson();
        if (StringUtils.isEmpty(nodeJson)) {
            log.error("ChampionChallenge node json is null");
            return XDecisionContext.getContext().getStateContext().setEngineResultEnum(EXCEPTION_NODE_NULL_END);
        }

        String[] split = nodeCode.split("_");
        String codeIndex=split[split.length-1];

        JSONArray jsonArray = JSONArray.parseArray(nodeJson);

        String nextNodeCode = engineNode.getNextNodeCodes();

//        PipelineContextHolder.setBranch(BranchTypeEnum.CHAMPION.name());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            int champion = jsonObject.getIntValue("champion");
            // 返回冠军分支对应的下一个节点
            String nextNode = jsonObject.getString("nextNode");
            if (StringUtils.isNotEmpty(nextNode)) {
                if (champion == 1) {
                    nextNodeCode = nextNode;
                } else {
                    log.info("进入挑战者分支,nodeCode={}", nextNode);
                    championChallengeHandler.asyncHandler(nodeCode,nextNode,codeIndex,i);
                }
            }
        }
        log.info("执行冠军挑战节点 runNode nodeCode:{},nextNode:{}", nodeCode, nextNodeCode);

        return XDecisionContext.getContext().getStateContext().setNextNodeCode(nextNodeCode);
    }



    @Override
    public boolean terminalCondition(NodeHandler nodeHandler, Map<String, Object> nodeOutput, Map<String, Object> nodeInput) {
        return false;
    }
}
